package com.kylin.module.admin.business;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;

import com.kylin.common.dict.orm.CatDict;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.common.util.MemObjFactory;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.admin.form.DictForm;

public class ManageDictBlogic {
	private int psgeSize = 15;
	protected static Logger log = Logger.getLogger(ManageDictBlogic.class);

	public DisplayPage getDictList(int pageNum, CriteriaInfo cinfo) throws BusinessException {
		log.info("search page " + pageNum);
		DisplayPage page = PageManager.getDisplayPage(psgeSize, pageNum, cinfo);
		return page;
	}

	public void addDict(DictForm form) throws BusinessException {
		log.debug("插入字典内容项: " + form.getName());
		List same = findSameDict(form);
		if(same != null && same.size() > 0)
			throw new BusinessException("msg.manage.saveDict.existsame");
		try{
		CatDict cat = (CatDict) BaseHibernateDAO.findById(CatDict.class, form.getCatId());
		Dict obj = new Dict();
		obj.setName(form.getName());
		obj.setDescription(form.getDescription());
		obj.setOrdering(form.getOrdering());
		if(form.getParent() == null || form.getParent().longValue() == 0)
			obj.setParent(null);
		else
			obj.setParent( (Dict)(BaseHibernateDAO.findById(Dict.class, form.getParent())) );
System.out.println("obj.parnet===="+obj.getParent());
		obj.setCatdict(cat);
		obj.setCreatedon(new Date());
		obj.setLanguage(form.getLanguage());
		BaseHibernateDAO.save(obj);
		MemObjFactory.reloadDict(cat);
		}catch(BusinessException e){
			throw new BusinessException("msg.manage.saveDict.fail",e);
		}
		
	}

	public void updateDict(DictForm form) throws BusinessException {
		log.debug("update字典内容项: " + form.getName());
		try{
			
			CatDict cat = (CatDict) BaseHibernateDAO.findById(CatDict.class, form.getCatId());
			Dict obj = (Dict) BaseHibernateDAO.findById(Dict.class, form.getId());
			obj.setName(form.getName());
			obj.setDescription(form.getDescription());
			obj.setOrdering(form.getOrdering());
			if(form.getParent() == null || form.getParent().longValue() == 0)
				obj.setParent(null);
			else
				obj.setParent( (Dict)(BaseHibernateDAO.findById(Dict.class, form.getParent())) );
	System.out.println("obj.parnet===="+obj.getParent());
			obj.setCatdict(cat);
			obj.setLastupdatedon(new Date());
			obj.setLanguage(form.getLanguage());
			BaseHibernateDAO.saveOrUpdate(obj);
			MemObjFactory.reloadDict(cat);
			}catch(BusinessException e){
				throw new BusinessException("msg.manage.updateDict.fail",e);
			}
		
	}	
	
	private List findSameDict(DictForm form){
		PropertyCondition[] pConditions = new PropertyCondition[] {
				new PropertyCondition("catdict.id", form.getCatId(),PropertyCondition.OPERATOR_EQ ),
				new PropertyCondition("name", form.getName(),PropertyCondition.OPERATOR_EQ )
		};
		CriteriaInfo cinfo = new CriteriaInfo(Dict.class, pConditions, Order.asc("ordering"));
		try {
			return BaseHibernateDAO.getCriteriaFromInfo(cinfo).list();
		} catch (BusinessException e) {
			e.printStackTrace();
			return null;
		}	
	}
	

	public Dict getDictById(Long id) throws BusinessException {
		return (Dict) BaseHibernateDAO.findById(Dict.class, id);
	}
	
	public void deleteDictById(Long id) throws BusinessException {
		Dict dict = getDictById(id);
		if(dict != null){
			List subs = BaseHibernateDAO.findByProperty(Dict.class, "parent", dict);
			for(int i=0;i<subs.size();i++){
				BaseHibernateDAO.delete(subs.get(i));
			}
			BaseHibernateDAO.deleteById(Dict.class, id);
			MemObjFactory.reloadDict(dict.getCatdict());
		}
	}
	
}
